<template>
  <div class="app-container">
    <div class="filter-container" align="center">
      <el-form :inline="true" :model="pageInfo" class="demo-form-inline">

        <el-form-item label="书名">
          <el-input v-model="pageInfo.bookName" placeholder="请输入小说书名" />
        </el-form-item>


        <!--        <el-form-item label="用户余额">-->
        <!--          <el-input v-model="pageInfo.money" placeholder="用户余额" />-->
        <!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" class="filter-item" icon="el-icon-search" @click="onSubmit">查询</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="filter-item" icon="el-icon-edit" @click="onAdd">添加章节</el-button>
        </el-form-item>


      </el-form>
    </div>
    <el-table :data="chapters" border style="width: 100%" stripe>
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="bookName"
        label="书名"
        width="200"
        align="center"
      />
      <el-table-column
        prop="chapter"
        label="章节"
        width="200"
        align="center"
      />

<!--      prop="chapters"-->
      <el-table-column  label="审核" align="center" :formatter="sta" width="100">

      </el-table-column>

      <el-table-column label="操作" align="center">
        <!--                          获取每一次循环时 的元素节点 (v-for) user in users 中的 user    -->
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary" icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)"
          ></el-button>
          <el-button
            size="mini"
            type="primary" icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    分页组件-->
    <el-pagination
      background
      :page-size="pageInfo.pageSize"
      :page-sizes="[5, 10, 20, 30]"
      :current-page="pageInfo.pageNum"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>


<script>
import axios from "axios";

export default {
  name: "UserMoneyView",
  data() {
    return {
      total: 0,
      pageInfo: {
        pageNum: 1,
        pageSize: 5,
        bookName:"",
      },
      chapters: [],



    }

  },
  // 前置执行方法在执行之前    beforeCreate()   axios.post(get)("http://localhost:8080/queryAll").then(...)
  created() { // 这是可以获取data中的数据后  再执行下面的axios
    this.getData()
  },
  methods: {
    sta(row) {
      if (row.state == 1) {
        return '通过'
      } else if (row.state == 2) {
        return '审核中'
      }else {
        return '未通过'
      }
    },
    // sta2(row){
    //   var arr=[]
    //   var roles=row.roleList
    //   for (const key in roles) {
    //     arr.push(roles[key].name)
    //   }
    //   return arr.join(",")
    // },

    // 修改余额
    handleEdit(index, row) {
      // alert(row.id)
      // 使用路由的push方法  跳转到修改余额页面
      this.$router.push({name: 'NovelChapterEditView', query: {id: row.id}})
    },


    // 修改每页条数
    handleSizeChange(val) {
      this.pageInfo.pageSize = val
      this.getData()
    },
    // 修改当前页数
    handleCurrentChange(val) {
      this.pageInfo.pageNum = val
      this.getData()
    },
    // 获取数据
    getData() {
      var formData = new FormData();
      for (const arr in this.pageInfo) {
        formData.append(arr, this.pageInfo[arr])
      }
      axios({
        //获取所有的用户的信息和他们的角色信息
        url: '/api/chapters/queryAll',
        method: 'post',
        data: formData,
        headers: {"Authorization": "Bearer " + this.$store.state.user.token}
      }).then(res => {
        // console.log(res.data.data.list[0].moduleList)
        // console.log(res.data.data)
        // this.money=res.data.data.list
        this.chapters=res.data.data.list
        // console.log(this.role)
        // for (const arr in this.role) {
        //     this.role.name=this.role[arr].roleList.name
        // }
        this.total = res.data.data.total
      })


    },
    // 查询
    onSubmit() {
      // alert(this.pageInfo.username)
      this.pageInfo.pageNum = 1 // 点击查询时  页码重置为1
      this.getData()
    },
    // 添加新角色
    onAdd() {
      this.$router.push('/novel/novelChapterAddView')
    },
    // 删除医生信息
    handleDelete(index, row) {
      // alert(row.id)
      // get post
      if (confirm('确定要删除吗')) {

        axios({
          url: '/api/chapters/delete/' + row.id,
          method: 'post',
          headers: {"Authorization": "Bearer " + this.$store.state.user.token}
        }).then(res => {
          if (res.data.code == 1) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            // 更新本页数据
            this.getData()
          } else {
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }
        })



      }
    }
    // this.$router.push({name:})


  }
}
</script>

<style scoped>

</style>
